/**
 * This class provides all the update methods to change the data in db4o database
 * @author Jianda Xu
 */
package db4oClass.daoClass;

import java.util.Date;

import general.appClass.StatisticApp;
import general.dbClass.*;

import com.db4o.ObjectContainer;

public class Db4oDaoUpdate {
	/**
	 * insert new category into database
	 * @param conn			connection with database
	 * @param value			new category's name
	 * @return				true if successfully,false if the category'name already exists in database
	 */
	public static boolean insertNewCategory(ObjectContainer conn,String value) {
		DBCategory category = new DBCategory(value);
		
		//check whether the category exists already in database
		if(Db4oDaoGet.getCategoryByName(conn, value) != null)
			return false;

		
		int id = Db4oDaoGet.nextCategoryId(conn);
		category.setNr(id);
		
		conn.store(category);
		conn.commit();
		return true;
	}
	
	/**
	 * insert new tag into database
	 * @param conn		connection with database
	 * @param value		new tag's name
	 * @return			true if successfully,false if the tag'name already exists in database
	 */
	public static boolean insertNewKeyword(ObjectContainer conn,String value) {
		DBKeyword tag = new DBKeyword(value);
		
		//check whether the tag exists already in database
		if(Db4oDaoGet.getTagByName(conn, value) != null)
			return false;
		
		int id = Db4oDaoGet.nextTagId(conn);
		tag.setNr(id);
		
		conn.store(tag);
		conn.commit();
		return true;
	}
	
	/**
	 * insert new photo into database
	 * @param container		connection with db4o database
	 * @param photo			new photo object
	 * @param data			photo's data
	 * @return
	 */
	public static void insertNewPhoto(ObjectContainer container,DBPicInfo photo,byte[] data) {
		int id = Db4oDaoGet.nextPhotoId(container);
		photo.setNr(id);
		
		DBPicData photoData 	= new DBPicData(id,data);
		DBThumbData thumbData 	= new DBThumbData(id,photo.getName(),data);
		
		container.store(photo);
		
		long begin = new Date().getTime();
		container.store(photoData);
		long end = new Date().getTime();
		
		container.store(thumbData);
		container.commit();
				
		long time = end - begin;
		StatisticApp.query("upload", "db4o", photoData.getSize(), time, 1);
	}
	
	/**
	 * change photo's information by photo id
	 * @param container		connection with db4o database
	 * @param photo			photo object
	 * @return 				false if photo id doesn't exist,true if update successful
	 */
	public static boolean updatePhotoById(ObjectContainer container,DBPicInfo photo) {
		DBPicInfo 	tmp 	= Db4oDaoGet.getPhotoInfoById(container, photo.getNr());
		
		if(tmp == null)
			return false;
		
		tmp.setName(photo.getName());
		tmp.setRate(photo.getRate());
		tmp.setCategories(photo.getCategories());
		tmp.setKeywords(photo.getKeywords());
		
		container.store(tmp);
		container.commit();
		
		return true;
	}
	
	
	/**
	 * change category's name by id
	 * @param container		connection with db4o database
	 * @param name			new category's name
	 * @param id			category's id
	 * @return				false if category's id doesn't exist,true if change successful
	 */
	public static boolean changeCategoryNameById(ObjectContainer container,String name,int id) {
		DBCategory category = Db4oDaoGet.getCategoryById(container, id);
		
		if(category == null)
			return false;
		
		category.setName(name);
		container.store(category);
		container.commit();
		
		return true;
	}
	
	/**
	 * change keyword's name by id
	 * @param container		connection with db4o database
	 * @param name			new keyword's name
	 * @param id			keyword's id
	 * @return				false if keyword's id doesn't exist,true if change successful
	 */
	public static boolean changeKeywordNameById(ObjectContainer container,String name,int id) {
		DBKeyword keyword = Db4oDaoGet.getTagById(container, id);
		
		if(keyword == null)
			return false;
		
		keyword.setWord(name);
		container.store(keyword);
		
		container.commit();
		return true;
	}

	/**
	 * delete photo by photo id 
	 * @param container		connection with db4o database
	 * @param id			photo id
	 */
	public static void deletePhotoById(ObjectContainer container, int id) {
		DBPicInfo 	photoInfo = Db4oDaoGet.getPhotoInfoById(container, id);
		DBPicData	photoData = Db4oDaoGet.getPhotoDataById(container, id);
		DBThumbData	thumbData = Db4oDaoGet.getThumbByPhotoId(container, id);
		
		container.delete(photoInfo);
		container.delete(photoData);
		container.delete(thumbData);
		container.delete(photoInfo.getExif());
		
		container.commit();
	}
	
	/**
	 * delete category by id;
	 * @param container
	 * @param id
	 */
	public static boolean deleteCategoryById(ObjectContainer container,int id) {
		DBCategory category = Db4oDaoGet.getCategoryById(container, id);
		
		if(category == null)
			return false;
		
		container.delete(category);
		container.commit();
		return true;
	}
	
	/**
	 * delete keyword by Id;
	 * @param container
	 * @param id
	 */
	public static boolean deleteKeywordById(ObjectContainer container,int id) {
		DBKeyword keyword = Db4oDaoGet.getTagById(container, id);
		
		if(keyword == null)
			return false;
		
		container.delete(keyword);
		container.commit();
		
		return true;
	}
}
